#include <ESP8266WiFi.h>

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif

const char* ssid = "好运来";  // 设置wifi和密码
const char* password = "13827676380";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(115200);  // 开始接受信息，传输速率为115200

  // prepare LED
  pinMode(D1, OUTPUT);  // D1脚位灯设置为输出灯
  digitalWrite(D1, LOW);  // 初始状态关闭灯

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print(F("Connecting to "));
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);  // 连接客户的wifi

  while (WiFi.status() != WL_CONNECTED) {  // 等待wifi连接
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();
  Serial.println(F("WiFi connected"));   // 连接成功

  // Start the server
  server.begin();
  Serial.println(F("Server started"));  // 准备就绪

  // Print the IP address
  Serial.println(WiFi.localIP());  // 打印网址
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) { return; }
  Serial.println(F("new client"));

  client.setTimeout(5000);  // default is 1000

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(F("request: "));
  Serial.println(req);

  // Match the request
  int val;
  if (req.indexOf(F("/gpio/0")) != -1) {
    val = LOW;
  } else if (req.indexOf(F("/gpio/1")) != -1) {
    val = HIGH;
  } else {
    Serial.println(F("invalid request"));
    val = digitalRead(D1);
  }

  // Set LED according to the request
  digitalWrite(D1, val);

  // Send the response to the client
  client.println(F("HTTP/1.1 200 OK"));
  client.println(F("Content-Type: text/html"));
  client.println();
  client.println(F("<!DOCTYPE HTML>"));
  client.println(F("<html>"));
  client.println(F("<head>"));
  client.println(F("<title>WiFi-lamb</title>"));
  client.println(F("<style>"));
  client.println(F("body {"));
  client.println(F("  font-family: Arial, sans-serif;"));
  client.println(F("  text-align: center;"));
  client.println(F("}"));
  client.println(F("h1 {"));
  client.println(F("  margin-top: 50px;"));
  client.println(F("  color: #333;"));
  client.println(F("}"));
  client.println(F(".button {"));
  client.println(F("  display: inline-block;"));
  client.println(F("  padding: 10px 20px;"));
  client.println(F("  font-size: 18px;"));
  client.println(F("  border-radius: 5px;"));
  client.println(F("  background-color: #4CAF50;"));
  client.println(F("  color: white;"));
  client.println(F("  text-decoration: none;"));
  client.println(F("  cursor: pointer;"));
  client.println(F("}"));
  client.println(F(".button-off {"));
  client.println(F("  background-color: #f44336;"));
  client.println(F("}"));
  client.println(F("</style>"));
  client.println(F("</head>"));
  client.println(F("<body>"));
  client.println(F("<h1>WiFi-lamb</h1>"));
  client.print(F("<a href=\"/gpio/1\" class=\"button\">"));
  client.print((val == HIGH) ? F("ON") : F("ON"));
  client.println(F("</a>"));
  client.print(F("<a href=\"/gpio/0\" class=\"button button-off\">"));
  client.print((val == LOW) ? F("OFF") : F("OFF"));
  client.println(F("</a>"));
  client.println(F("</body>"));
  client.println(F("</html>"));

  // read/ignore the rest of the request
  while (client.available()) {
    // byte by byte is not very efficient
    client.read();
  }
}